// import { Dialog } from 'vant'
import store from '@/store'
import $AUTH from '@/utils/auth'
import {
  isPc,
  isMobile,
  openNewWindow,
  getPlatform,
  isWxBrowser
} from '@/utils'

import {
  pauseAllAudio,
  toggleMenuOrPointAudio
} from '@/utils/audio'


let gotoAuth = () => {
  $AUTH.weixinAuth($VM.$route.params.panoId)
}

const hotspotStrategies = {
  '1': function(data) {
    if (~data.indexOf('scene_')) {
      data = data.split('scene_')[1]
    }
    // console.log(data)
    $VM.$vgo.emit('sync:scene', data)
  },
  '2': function(data) {
    $VM.$vgo.emit('hotpoint:productlink', data)
  },
  '3': function(data, abank) {
    pauseAllAudio()
    $VM.$krp.controlsAllVideoin()
    if(data.toString().indexOf('//#') == -1){
      data =  data.replace('/#','//#')
    } else {
      data =  data.replace('//','/')
    }

    //房地产方案特殊处理
    if(window.top.location.href.indexOf('/estate/') && window.location.href.indexOf('/panoview/') && window.top.panoView){
      window.top.panoView.src = data
      return
    }

    if (data.includes('.edu') ||
      data.indexOf('weixin.qq.com') != -1 || data.indexOf('weixin.com') != -1 || data.indexOf('qq.com') != -1 ||
      data.indexOf('fjnx.com') != -1
      //|| data.indexOf($globalconfig.DOMAIN) != -1
    ) {
      window.open(data)
      // window.location.href = data
      if(!isPc()) {
        setTimeout(() => {
          window.location.reload()
        }, 500)
      }
    } else if (abank || openNewWindow(data)) {
      // 手机端无法再新窗口打开，强制刷新
      if (!getPlatform()) {
        window.location = data
        // setTimeout(function() {
        //   window.location.reload()
        // }, 500)
      } else {
        window.open(data, '_blank')
      }
    } else {
      $VM.$vgo.emit('iframe:link', data)
    }
  },
  '4': function(data) {

    console.log('全景跳转', data)
  },
  '5': function(data) {
    $VM.$vgo.emit('iframe:3d', data)
  },
  '6': function(data) {
    // 图文
    store.dispatch('fetchGraphicData', data)
  },
  '7': function(data) {
    console.log('图片')
    if (~data.indexOf('|')) {
      data = data.split('|')
    }

    if (!~data.indexOf('http')) {
      data = $globalconfig.URLS.API + data[1]
    }
    data = data.replace('s_', '')
    $VM.$vgo.emit('iframe:image', data)
  },
  '8': function(data) {
    pauseAllAudio()
    $VM.$krp.controlsAllVideoin()
    // console.log('多媒体', data)
    if (data.indexOf('|') !== -1) {
      data = data.split('|')[1]
    }
    // 判断是否全路径
    data = data.indexOf('http') > -1 ? data : $globalconfig.URLS.API + data
    // console.log(data)
    toggleMenuOrPointAudio(data)
  },
  '9': function(data) {
    const videoUrl = data.split('|')[1]
    // console.log('视频', videoUrl)
    pauseAllAudio()
    $VM.$krp.controlsAllVideoin()

    $VM.$vgo.emit('iframe:video', videoUrl)
  },
  '10': function(data) {
    if (isWxBrowser()) {
      let wxurl = data
      let token = $AUTH.getUserCookie('access_token')
      if (!token) {
        gotoAuth()
      } else {
        // 智能表单和微商城打开新页面
        if (data.indexOf('form-aiview') != -1 || data.indexOf('from=store') != -1) {
          window.location.href = wxurl
        } else if (!$globalconfig.IframeBlank) {
          $VM.$vgo.emit('iframe:link', wxurl)
        } else {
          window.location.href = wxurl
        }
      }

      // console.log('插件', data)
      // let openid = ''
      // if (store && store.getters && store.getters.userInfo) {
      //   openid = store.getters.userInfo.wx_openid
      // }

      // if (!openId) {
      //   gotoAuth()
      // } else {
      //   let avatar = (store && store.getters && store.getters.userInfo) ? (store.getters.userInfo.full_avatar || store.getters.userInfo.avatar) : ''
      //   let nickname = (store && store.getters && store.getters.userInfo) ? store.getters.userInfo.nickname : ''
      //   let wxurl = data + `&openid=${openId}&wechatid=${openId}&nickname=${nickname}&headimgurl=${avatar}`
      //   // 智能表单和微商城打开新页面
      //   if (data.indexOf('form-aiview') != -1 || data.indexOf('from=store') != -1) {
      //     window.location.href = wxurl
      //   } else if (!$globalconfig.IframeBlank) {
      //     $VM.$vgo.emit('iframe:link', wxurl)
      //   } else {
      //     window.location.href = wxurl
      //   }
      // }
    } else {
      console.log(data)
      $VM.$vgo.emit('iframe:plugin', data)
    }
  },
  '11': function(data) {
    console.log('商品', data)
    store.dispatch('fetchGoodsData', data)
    if (getPlatform()) {
      $VM.$vgo.emit('modal:goods')
    } else {
      $VM.$router.push(`/panoview/${$VM.$route.params.panoId}/goods-detail?id=${data}`)
    }
  },
  '12': function(data) {
    // 幻灯片
    $VM.$vgo.emit('iframe:swiper', filterSwiper(data))
  },
  '15': function(data, abank, atv, ath, iswritephone) {
    // iswritephone=1,0 是否填写手机号
    if ($VM.$AUTH.checkLogin()) {
      if (iswritephone) {
        // console.log(data)
        $VM.$vgo.emit('welfare:phone', data)
      } else {
        store.dispatch('grabWelfare', { id: data, phone: '' })
      }
    }
  },
  '17': function(data, abank, atv, ath) {
    $VM.$krp.callScene(`lookto(${ath},${atv}`)
    // 过度视频
    if(isMobile()) {
      pauseAllAudio()
      $VM.$krp.controlsAllVideoin()
      let obj = {
        sceneId: data.split('|')[0],
        videoUrl: data.split('|')[1],
      }
      setTimeout(() => {
        $VM.$vgo.emit('iframe:transiton-video', obj)
      }, 10)
    } else {
      $VM.$vgo.tip('该热点仅在手机端可查看', 'warning')
    }
  },
  '18': function(data) {
    pauseAllAudio()
    $VM.$krp.controlsAllVideoin()
    // console.log(data)
    // 多功能热点
    if (data) {
      // console.log(data)
      $VM.$vgo.emit('dialog:mutil', {data: data})
    } else {
      $VM.$vgo.tip('没有数据可展示')
    }
  },
  '19': function(sceneId) {
    // 航拍热点
    // console.log(sceneId)
    $VM.$vgo.emit('sync:scene', sceneId)
  },
  '21': function(data) {
    $VM.$vgo.emit('directory:hotspot', {data: JSON.parse(data)})
  },
}

export const handleHotspot = (item) => {
  // AR动画和内嵌文字和场景标注不做处理
  if (item.type_id === 13 || item.type_id == 14 || item.type_id == 16) {return}

  let data
  let abank
  let iswritephone
  // 红包处理
  if(item.type_id == '15') {
    data = item.plugname
    iswritephone = item.iswritephone
  } else if(item.type_id == '3') {
    data = item.datastr
    abank = item.abank
  } else if (item.type_id == '19') {
    // 航拍热点
    let obj = JSON.parse(item.datastr)
    data = obj.scene_id
  } else {
    data = item.datastr
  }
  return hotspotStrategies[item.type_id](data, abank, item.atv, item.ath, iswritephone)
}

export const filterSwiper = (data) => {
  // 1: Pc : 2 app
  let model = getPlatform() ? 1 : 2
  let ary = []

  data = data.replace(/s_/g, '')
  if (~data.indexOf('|')) {
    ary = data.split('|')
  } else {
    ary.push(data)
  }

  ary = ary.map((it) => {
    if (!~it.indexOf('http')) {
      it = $globalconfig.URLS.API + it
    }
    return it
  })
  return { model: model, data: ary }
}
